package com.js.nickcar.module.addcar.check;

import com.js.nickcar.api.HttpHelpter;
import com.js.nickcar.api.OptionConfig;
import com.js.nickcar.api.OptionsHelper;
import com.js.nickcar.api.UrlConfig;
import com.js.nickcar.api.UrlUtils;
import com.js.nickcar.base.ApiCallback;
import com.js.nickcar.base.BasePresenter;
import com.js.nickcar.base.HttpResultFunc;
import com.js.nickcar.entity.local.BaseCarInfoEntity;
import com.js.nickcar.entity.remote.ApiResultBean;
import com.js.nickcar.entity.remote.OptionBean;
import com.zyzd.commontools.log.TLog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.Subscriber;


/**
 * Created by LZY on 2017/10/5.
 */
public class CheckCarPresenter extends BasePresenter<CheckCarView>
        implements BaseCarInfoEntity.ItemType, BaseCarInfoEntity.Param, OptionConfig {

    private String mCarId;

    private OptionBean optionCheckLicenseType;
    private OptionBean optionCheckTechStatus;
    private OptionBean optionCheckFixStatus;
    private OptionBean optionCheckUsage;
    private OptionBean optionCheckStatus;

    public void getOptions(String[] types) {
        StringBuilder sb = new StringBuilder();
        if (types != null && types.length > 0) {
            sb.append(types[0]);
            for (int i = 1; i < types.length; i++) {
                sb.append('|').append(types[i]);
            }
        }
        Map<String, String> params = UrlUtils.getBaseRequestMap(UrlConfig.API_COMMON_OPTIONS);
        params.put("type",sb.toString());
        Observable<ApiResultBean<HashMap<String, ArrayList<OptionBean>>>> options = HttpHelpter.getApiService().getOptions(params);
        addSubscription(options.map(new HttpResultFunc<HashMap<String, ArrayList<OptionBean>>>()), getOptionSubscriber());
    }

    private Subscriber<HashMap<String, ArrayList<OptionBean>>> getOptionSubscriber() {
        return new ApiCallback<HashMap<String, ArrayList<OptionBean>>>() {
            @Override
            public void onSuccess(HashMap<String, ArrayList<OptionBean>> data) {
                //获取参数信息成功，更新本地缓存，通知页面更新
                OptionsHelper.updateOption(data);
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }

            @Override
            public void onFinish() {

            }
        };
    }

    public List<BaseCarInfoEntity> getData() {
        ArrayList<BaseCarInfoEntity> lists = new ArrayList<>();
        lists.add(new BaseCarInfoEntity("证件类型", TYPE_SELECT, LICENSE_TYPE));
        lists.add(new BaseCarInfoEntity("技术状况", TYPE_SELECT, TECH_STATUS));
        lists.add(new BaseCarInfoEntity("维修保养", TYPE_SELECT, FIX_STATUS));
        lists.add(new BaseCarInfoEntity("工作性质", TYPE_SELECT, USAGE));
        lists.add(new BaseCarInfoEntity("综合车况", TYPE_SELECT, STATUS));
        lists.add(new BaseCarInfoEntity("下一步", TYPE_NEXT, NEXT));
        return lists;
    }

    public void commitCheckCar(Map<String, String> map) {
        TLog.d("zyzd", "CheckCarPresenter>>commitCheckCar--> " + map);
        addSubscription(HttpHelpter.getApiService()
                .commitCarCheck(map)
                .map(new HttpResultFunc()), getCommitCheckCarSubcriber());
    }

    private Subscriber getCommitCheckCarSubcriber() {
        return new ApiCallback() {
            @Override
            public void onSuccess(Object model) {
                //提交成功
                TLog.d("zyzd", "CheckCarPresenter>>onSuccess--> " );
                mView.commitCheckCarSubcriberSuccess(mCarId);
            }

            @Override
            public void onFailure(String msg) {
                TLog.d("zyzd", "CheckCarPresenter>>onFailure--> " + msg);
            }

            @Override
            public void onFinish() {

            }
        };
    }

    public void setCarId(String carId) {
        this.mCarId = carId;
    }

    public String getCarId() {
        return this.mCarId;
    }


    public ArrayList<OptionBean> getCheckLicenseTypeData() {
        return OptionsHelper.getOptions().get(CAR_CHECK_LICENSE_TYPE);
    }

    public ArrayList<OptionBean> getCheckTechStatusData() {
        return OptionsHelper.getOptions().get(CAR_CHECK_TECH_STATUS);
    }

    public ArrayList<OptionBean> getCheckFixStatusData() {
        return OptionsHelper.getOptions().get(CAR_CHECK_FIX_STATUS);
    }


    public ArrayList<OptionBean> getCheckUsageData() {
        return OptionsHelper.getOptions().get(CAR_CHECK_USAGE);
    }

    public ArrayList<OptionBean> getCheckStatusData() {
        return OptionsHelper.getOptions().get(CAR_CHECK_STATUS);
    }


    public OptionBean getOptionCheckFixStatus() {
        return optionCheckFixStatus;
    }

    public void setOptionCheckFixStatus(OptionBean optionCheckFixStatus) {
        this.optionCheckFixStatus = optionCheckFixStatus;
    }

    public OptionBean getOptionCheckLicenseType() {
        return optionCheckLicenseType;
    }

    public void setOptionCheckLicenseType(OptionBean optionCheckLicenseType) {
        this.optionCheckLicenseType = optionCheckLicenseType;
    }


    public OptionBean getOptionCheckTechStatus() {
        return optionCheckTechStatus;
    }

    public void setOptionCheckTechStatus(OptionBean optionCheckTechStatus) {
        this.optionCheckTechStatus = optionCheckTechStatus;
    }

    public OptionBean getOptionCheckUsage() {
        return optionCheckUsage;
    }

    public void setOptionCheckUsage(OptionBean optionCheckUsage) {
        this.optionCheckUsage = optionCheckUsage;
    }

    public OptionBean getOptionCheckStatus() {
        return optionCheckStatus;
    }

    public void setOptionCheckStatus(OptionBean optionCheckStatus) {
        this.optionCheckStatus = optionCheckStatus;
    }
}
